<?php

/**
 * Description of Account
 *
 * @author markos
 */
class VirtualLabs_Rest_VirtualLabs_Project {

    private $_debug_log;
    private $_error_log;

    public function __construct() {
        $this->_error_log = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
        $this->_debug_log = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
    }

    public function updateVmLimit($id, $vmLimit) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "updateResourceLimit";
        $url["resourcetype"] = 0;
        $url["projectid"] = $id;
        $url["max"] = $vmLimit;
        try {
            $v = new VirtualLabs_Rest_HttpClient_Client();
            $response = $v->call($url);
            $this->_debug_log->debug("updateVmLimit id[$id] limit[$vmLimit]");
            $updateProjectResponse = Zend_Json_Decoder::decode($response);
            $this->_debug_log->debug(print_r($updateProjectResponse, true));
            return $updateProjectResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }
    
    public function updateVolumeLimit($id, $volumeLimit) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "updateResourceLimit";
        $url["resourcetype"] = 2;
        $url["projectid"] = $id;
        $url["max"] = $volumeLimit;
        try {
            $v = new VirtualLabs_Rest_HttpClient_Client();
            $response = $v->call($url);
            $this->_debug_log->debug("updateVmLimit id[$id] limit[$volumeLimit]");
            $updateProjectResponse = Zend_Json_Decoder::decode($response);
            $this->_debug_log->debug(print_r($updateProjectResponse, true));
            return $updateProjectResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    /**
     * Lista las reglas de forward de puertos para el laboratorio virtual.
     * 
     * @param type $projectId codigo de proyecto en el cloudstack correspondiente al laboratorio virtual
     * @return type
     */
    public function listPortForwardingRules($projectId) {
        //

        $url = array();
        $url["response"] = "json";
        $url["command"] = "listPortForwardingRules";
        $url["projectid"] = $projectId;

        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
        echo "<h2>JSON</h2>$response";
        echo "<h2>Array PHP</h2>";
        $listIpForwardingRulesResponse = Zend_Json_Decoder::decode($response);
        return $listIpForwardingRulesResponse;
    }

    /**
     * Crea un nuevo proyecto en el cloudstack que se asocia con un laboratorio.
     * 
     * @param string $displayText 
     * @param string $name username para el account 
     * @param string $accountId password del usuario para acceso al cloudstack
     * @return type array() los datos del proyecto creado
     */
    public function createProject($displayText, $name, $accountId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "createProject";
        $url["displaytext"] = $displayText;
        $url["name"] = $name;
        $url["account"] = $accountId;
        try {
            $v = new VirtualLabs_Rest_HttpClient_Client();
            $response = $v->call($url);
            $createProjectResponse = Zend_Json_Decoder::decode($response);
            return $createProjectResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    /**
     * Deletes a project
     * 
     */
    public function deleteProject($id) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "deleteProject";
        $url["id"] = $id;
        try {
            $v = new VirtualLabs_Rest_HttpClient_Client();
            $response = $v->call($url);
            return Zend_Json_Decoder::decode($response);
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    public function suspendProject($id) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "suspendProject";
        $url["id"] = $id;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $registerResponse = Zend_Json_Decoder::decode($response);
            return $registerResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    /**
     * 
     */
    public function addAccountToProject($projectId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "addAccountToProject";
        $url["id"] = $projectId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $registerResponse = Zend_Json_Decoder::decode($response);
            return $registerResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    /**
     * 
     */
    public function listProject($projectId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "listProjects";
        $url["id"] = $projectId;
        $url["isrecursive"] = true;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $registerResponse = Zend_Json_Decoder::decode($response);
            $this->_debug_log->debug(print_r($registerResponse, true));
            return $registerResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    public function listVRouter($projectId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "listRouters";
        $url["projectid"] = $projectId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $registerResponse = Zend_Json_Decoder::decode($response);
            $this->_debug_log->debug(print_r($registerResponse, true));
            return $registerResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

    public function destroyVRouter($id) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "destroyRouters";
        $url["id"] = $id;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        try {
            $response = $v->call($url);
            $registerResponse = Zend_Json_Decoder::decode($response);
            $this->_debug_log->debug(print_r($registerResponse, true));
            return $registerResponse;
        } catch (Exception $e) {
            $this->_error_log->err($e->getTraceAsString);
            throw $e;
        }
    }

}
?>

